package com.djinfo.work.dao.impl;
import com.djinfo.work.common.dao.impl.BaseDaoImpl;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.NoticeDao;
import com.djinfo.work.entity.SysNoticeEntity;
import com.djinfo.work.entity.TbSysUsers;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * 公告
 */
@Repository("NoticeDao")
public class NoticeDaoImpl extends BaseDaoImpl implements NoticeDao {

	@Override
	public SysNoticeEntity getNoticeById(String id) {
		return (SysNoticeEntity) super.getByPrimaryKey(SysNoticeEntity.class, id);
	}

	@Override
	public List<SysNoticeEntity> page(SysNoticeEntity dicSysNoticeEntity, Integer pageNum, Integer pageSize) {
		String hql = "";

		if (StringUtil.isNotEmpty(dicSysNoticeEntity.getTitle()) || StringUtil.isNotEmpty(dicSysNoticeEntity.getStatus())) {

			if (dicSysNoticeEntity.getStatus() == null && StringUtil.isNotEmpty(dicSysNoticeEntity.getTitle())) {
				hql = "from SysNoticeEntity where title like '%" + dicSysNoticeEntity.getTitle() + "%'";
			}
			if (("").equals(dicSysNoticeEntity.getTitle()) && StringUtil.isNotEmpty(dicSysNoticeEntity.getStatus())) {
				hql = "from SysNoticeEntity where status=" + dicSysNoticeEntity.getStatus() + "";
			}
			if (StringUtil.isNotEmpty(dicSysNoticeEntity.getTitle()) && StringUtil.isNotEmpty(dicSysNoticeEntity.getStatus())) {
				hql = "from SysNoticeEntity where title like '%" + dicSysNoticeEntity.getTitle() + "%' and status=" + dicSysNoticeEntity.getStatus() + "";
			}
		} else {
			hql = " from SysNoticeEntity where 1=1 ";
		}
		return (List<SysNoticeEntity>) super.pageQuery(hql, pageNum, pageSize);
	}

	@Override
	public List<SysNoticeEntity> list() {
		String hql = " from SysNoticeEntity where 1=1 ";
		return (List<SysNoticeEntity>) super.getList(hql);
	}

	@Override
	public Integer getAllRowCount() {
		String hql = "select count(*) from SysNoticeEntity ";
		return super.getUniqueObject(hql).hashCode();
	}

	@Override
	public void updateStatus(String id, HttpSession session) {
		SysNoticeEntity sysNoticeEntity = super.get(SysNoticeEntity.class, id);
		String userid = (String) session.getAttribute("userId");
		TbSysUsers tbSysUsers = super.get(TbSysUsers.class,userid);
		String Id = tbSysUsers.getPkUserId();
		String nowTime = StringUtil.getNowTime();
		sysNoticeEntity.setUpdateTime(nowTime);
		sysNoticeEntity.setUpdateUser(Id);
		String sql = "";
		if (sysNoticeEntity.getStatus() == 0) {//待定变发布
			sysNoticeEntity.setStatus(1);
//			sql = "update tb_sys_notice set status='1' where id='" + id + "'";
		}else{
			sysNoticeEntity.setStatus(0);
		}
//		if (sysNoticeEntity.getStatus() == 1) {//发布变待定
//
////			sql = "update tb_sys_notice set status='0' where id='" + id + "'";
//		}

		this.update(sysNoticeEntity);
//		super.updateBySqlString(sql);
	}

	@Override
	public Integer getCountByCondition(SysNoticeEntity dicSysNoticeEntity) {
		String hql = "";
		if (StringUtil.isNotEmpty(dicSysNoticeEntity.getTitle()) || StringUtil.isNotEmpty(dicSysNoticeEntity.getStatus())) {
			if (dicSysNoticeEntity.getStatus() == null && StringUtil.isNotEmpty(dicSysNoticeEntity.getTitle())) {
				hql = "select count(*) from SysNoticeEntity where title like '%" + dicSysNoticeEntity.getTitle() + "%'";
			}
			if (("").equals(dicSysNoticeEntity.getTitle()) && StringUtil.isNotEmpty(dicSysNoticeEntity.getStatus())) {
				hql = "select count(*) from SysNoticeEntity where status=" + dicSysNoticeEntity.getStatus() + "";
			}
			if (StringUtil.isNotEmpty(dicSysNoticeEntity.getTitle()) && StringUtil.isNotEmpty(dicSysNoticeEntity.getStatus())) {
				hql = "select count(*) from SysNoticeEntity where title like '%" + dicSysNoticeEntity.getTitle() + "%' and status=" + dicSysNoticeEntity.getStatus() + "";
			}
		}
		return super.getUniqueObject(hql).hashCode();
	}
}

